package com.baizhi.action;

import cn.hutool.captcha.CircleCaptcha;
import com.baizhi.entity.User;
import com.baizhi.service.UserService;
import com.baizhi.service.impl.UserServiceImpl;
import javafx.application.Application;
import javafx.stage.Stage;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;
import java.util.ServiceConfigurationError;

public class UserAction{

    private UserService us = new UserServiceImpl();
    private User user;
    private String captcha;
    private String captchaRegist;

    public String login(){
        User selectUser = us.selectByName(this.user.getUserName());
        if(selectUser!=null&&!(user.getPassword().equals(""))&&selectUser.getPassword().equals(user.getPassword())){
            HttpSession session = ServletActionContext.getRequest().getSession();
            CircleCaptcha captchaC = (CircleCaptcha) session.getAttribute("captcha");
            boolean verify = captchaC.verify(captcha);

            if(verify/*captcha.equals("1234")*/){
                session.setAttribute("user",selectUser);
                return "login up";
            }else{
                return "login down";
            }
        }else{
            return "login down";
        }
    }

    public String regist(){
        User selectUser = us.selectByName(this.user.getUserName());
        if(selectUser == null){
            HttpSession session = ServletActionContext.getRequest().getSession();
            CircleCaptcha captchaRegistC = (CircleCaptcha) session.getAttribute("captchaRegist");
            boolean verify = captchaRegistC.verify(captchaRegist);
            if(verify){
                User u = new User(null,user.getUserName(),user.getPassword(),user.getUserCardId(),user.getUserTel(),user.getUserAddress(),user.getUserPostNumber(),0,"","");
                us.insert(u);
                return "regist up";
            }else{
                return "regist down";
            }
        }else{
            return "regist down";
        }
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String getCaptcha() {
        return captcha;
    }

    public void setCaptcha(String captcha) {
        this.captcha = captcha;
    }

    public String getCaptchaRegist() {
        return captchaRegist;
    }

    public void setCaptchaRegist(String captchaRegist) {
        this.captchaRegist = captchaRegist;
    }
}
